package com.sun.dailyprj.chery;


import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;

import com.sun.dailyprj.R;
import com.sun.dailyprj.androidx.room.RoomAppDatabase;
import com.sun.dailyprj.androidx.room.dao.UserDao;
import com.sun.dailyprj.androidx.room.entity.User;
import com.sun.dailyprj.chery.view.ExpandableTextView;
import com.sun.dailyprj.util.Logger;

import java.util.List;
import java.util.Random;

public class CheryEntryActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView mTVComparison;
    private Button mBtnUpdateText;
    private Button mBtnToListView;
    private TextSwitcher chery_ts;
    private String[] mSwitchArray;
    int i =0;

    private ExpandableTextView mETV;
    private CharSequence[] mPoems = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chery_main);

        mPoems = getResources().getStringArray(R.array.poems);

        mTVComparison = (TextView) this.findViewById(R.id.tv_comparison);
        mBtnUpdateText = (Button) this.findViewById(R.id.button_update_text);
        mBtnToListView = (Button) this.findViewById(R.id.button_to_list_view);
        mETV = (ExpandableTextView) this.findViewById(R.id.etv);
        mETV.setExpandListener(new ExpandListener());

        mBtnUpdateText.setOnClickListener(this);
        mBtnToListView.setOnClickListener(this);

        // 测试添加OnClickListener的情况，功能正常。添加外部的onClick事件后，原来的点击toggle功能自动屏蔽，
        // 点击尾部的ClickableSpan仍然有效
       /* mETV.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                switch(mETV.getExpandState()) {
                    case ExpandableTextView.STATE_SHRINK:
                        Toast.makeText(getApplicationContext(), "ExpandableTextView clicked, STATE_SHRINK",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case ExpandableTextView.STATE_EXPAND:
                        Toast.makeText(getApplicationContext(), "ExpandableTextView clicked, STATE_EXPAND",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });*/
        mETV.setExpandListener(new ExpandableTextView.OnExpandListener(){
            @Override
            public void onExpand(ExpandableTextView view) {
                Toast.makeText(getApplicationContext(), "ExpandableTextView clicked, STATE_EXPAND",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onShrink(ExpandableTextView view) {
                Toast.makeText(getApplicationContext(), "ExpandableTextView clicked, STATE_SHRINK",
                        Toast.LENGTH_SHORT).show();
            }
        });
        Logger.debug("start settext() ");
//        mETV.setText(getResources().getString(R.string.txt_data_binding));
//        mETV.setText(mPoems[0]);//在ExpandableTextView在创建完成之前改变文字，功能正常


        test().observe(this, new Observer<List<User>>(){
            @Override
            public void onChanged(List<User> users) {
                Logger.debug("users:" + users.toString());
            }
        });

        mSwitchArray = getResources().getStringArray(R.array.textswitch_array);
        chery_ts = findViewById(R.id.chery_ts);
        chery_ts.setFactory(new ViewSwitcher.ViewFactory(){
            @RequiresApi(api = Build.VERSION_CODES.M)
            public View makeView() {
                TextView tv = new TextView(CheryEntryActivity.this);
                tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24);
                tv.setTextColor(getColor(R.color.color_common_txt));
                return tv;
            }
        });

        new Thread(new Runnable(){
            @Override
            public void run() {
                for(int j=0 ;j<1000;j++){
                    i = j;
                    runOnUiThread(new Runnable(){
                        @Override
                        public void run() {
                            chery_ts.setText(mSwitchArray[i %mSwitchArray.length]);
                        }
                    });
                    try{
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();


    }

    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.button_to_list_view:
                gotoCheckInListView();
                break;
            case R.id.button_update_text:
                updateText();
                break;
        }
    }

    private void gotoCheckInListView() {
        Intent intent = new Intent(CheryEntryActivity.this, ActivityListView.class);
        startActivity(intent);
    }

    private Random mRandom = new Random();
    private int prevRandomInt = -1;
    private int currRandomInt = -1;

    private void updateText() {
        currRandomInt = mRandom.nextInt(mPoems.length);
        while (prevRandomInt == currRandomInt) {
            currRandomInt = mRandom.nextInt(mPoems.length);
        }
        prevRandomInt = currRandomInt;
        CharSequence newCS = mPoems[currRandomInt];

        mTVComparison.setText(newCS);//作为对比示例
        mETV.setText(newCS);//效果显示
    }

    class ExpandListener implements ExpandableTextView.OnExpandListener{
        @Override
        public void onExpand(ExpandableTextView view) {
            Logger.debug("onExpand() ");
//            Intent intent = new Intent(CheryEntryActivity.this, ActivityListView.class);
//            startActivity(intent);
        }

        @Override
        public void onShrink(ExpandableTextView view) {
//            Intent intent = new Intent(CheryEntryActivity.this, ActivityListView.class);
//            startActivity(intent);
            Logger.debug("onShrink() ");
        }
    }

    private MediatorLiveData<List<User>> test() {
        MediatorLiveData<List<User>> observer = new MediatorLiveData<>();
        observer.addSource(RoomAppDatabase.getInstance(this).getUserDao().getAllUsers(),
                new Observer<List<User>>(){
                    @Override
                    public void onChanged(List<User> users) {
                        if(users.size() == 0){
                            User user = new User();
                            user.setName("sunyanpeng");
                            user.setSex("male");
                            users.add(user);
                        }
                        observer.postValue(users);
                    }
                });
        return observer;
    }
}
